package com.zzedu.common.utils.http;

import okhttp3.OkHttpClient;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.util.concurrent.TimeUnit;

/**
 * project_name zebra-cash
 * package_name com.zebra.acc_dxhl
 *
 * @author chenqiang
 * @version 1.0
 * datetime 2019/07/12 18:09
 * description :: 创建OkHttp 单例
 */
public class OkHttp3Singleton {
    private static volatile OkHttpClient singletonHttps;
    private OkHttp3Singleton(){
    }

    private static final class SingletonHolder {
        private static final OkHttpClient SINGLETON = new OkHttpClient();
    }

    /**
    * description :: 获取OkHttp3 http client
    * @param: []
    * @return: okhttp3.OkHttpClient
    */
    protected static OkHttpClient getInstance() {
        return SingletonHolder.SINGLETON;
    }

    /**
    * description :: 获取OkHttp3 https client
    * @param: []
    * @return: okhttp3.OkHttpClient
    */
    protected static OkHttpClient getInstanceHttps() {
        if(singletonHttps == null){
            synchronized (OkHttp3Singleton.class)
            {
                if (singletonHttps == null)
                {
                    try {
                        final TrustManager[] trustAllCerts = new TrustManager[]{
                                new X509TrustManager() {
                                    @Override
                                    public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                                    }

                                    @Override
                                    public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
                                    }

                                    @Override
                                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                                        return new java.security.cert.X509Certificate[]{};
                                    }
                                }
                        };
                        final SSLContext sslContext = SSLContext.getInstance("SSL");
                        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
                        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
                        OkHttpClient.Builder builder = new OkHttpClient.Builder();
                        builder.sslSocketFactory(sslSocketFactory,(X509TrustManager) trustAllCerts[0]);
                        builder.hostnameVerifier((hostname, session) -> true);
                        singletonHttps = builder.connectTimeout(10,TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return singletonHttps;
    }
}
